package com.banciyuan.bcywebview.biz.acgwork;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.daoexample.model.AcgItem;

/**
 * Created by hy on 2015/6/30.
 */
public class MineAcgAdapter extends BaseAdapter {
    private Context context;
    private List<AcgItem> data = new ArrayList<AcgItem>();
    private ImageLoader imageLoader;
    private LayoutInflater inflater;

    public MineAcgAdapter(Context context, List<AcgItem> data) {
        this.context = context;
        this.data = data;
        inflater = LayoutInflater.from(context);
        imageLoader = ImageLoader.getInstance();
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {

        WorkViewHolder workViewHolder = null;
        final AcgItem item = data.get(position);

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.search_work_header, null);
            workViewHolder = new WorkViewHolder(convertView);
            convertView.setTag(workViewHolder);
        } else {
            workViewHolder = (WorkViewHolder) convertView.getTag();
        }

        if (!TextUtils.isEmpty(item.getImg_src())) {
            workViewHolder.head_img.setVisibility(View.VISIBLE);
            workViewHolder.head_tag.setVisibility(View.GONE);
            imageLoader.displayImage(item.getImg_src(), workViewHolder.head_img, ImageOptions.options);
        } else {
            workViewHolder.head_img.setVisibility(View.GONE);
            workViewHolder.head_tag.setVisibility(View.VISIBLE);
        }

        if (!TextUtils.isEmpty(item.getReal_name())) {
            workViewHolder.head_text.setText(item.getReal_name());
        }
        workViewHolder.work_split.setVisibility(View.GONE);
        return convertView;
    }

    public void setData(List<AcgItem> data) {
        this.data = data;
    }

    private class WorkViewHolder {
        private ImageView head_img;
        private ImageView head_tag;
        private TextView head_text;
        private LinearLayout work_split;

        public WorkViewHolder(View convertView) {
            head_img = (ImageView) convertView.findViewById(R.id.iv_work);
            head_text = (TextView) convertView.findViewById(R.id.tv_workname);
            head_tag = (ImageView) convertView.findViewById(R.id.iv_tag);
            work_split = (LinearLayout) convertView.findViewById(R.id.work_split);
        }
    }
}
